﻿<!DOCTYPE html>
<!--区域管理-->
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="/static/js/common.js"></script>
    <link href="../static/js/vue-treeselect/vue-treeselect.css" rel="stylesheet" />
    <script src="../static/js/vue-treeselect/vue-treeselect.js"></script>
    <style>
        .vue-treeselect__control {
            height: 100%;
            border: none;
            background: none;
        }

        .vue-treeselect__placeholder {
            top: -3px;
        }

        .vue-treeselect__single-value {
            top: -3px;
        }
    </style>
    <style>
        .el-table__body-wrapper .el-table_1_column_2, .el-table_1_column_4, .el-table_1_column_5, .el-table_1_column_6, .el-table_1_column_7 {
            text-align: center !important;
        }

        .el-table__body-wrapper .el-table_1_column_3 {
            text-align: left !important;
        }

        .el-dialog__body {
            padding: 10px !important;
        }
    </style>
</head>

<body>
    <div id="app" v-cloak>
        <el-row>
            <el-col :span="24">
                <el-form label-position="right" ref="form" :inline="true" :model="formInline" class="demo-form-inline"
                         style="margin-bottom: 15px;">
                    <el-row>
                        <el-col class="head-tool">
                            <span>
                                <btn :text="col.btnSelect" imgsrc="/static/images/search.png" linetype="3"
                                     @click="onSubmit"></btn>
                                <btn :text="col.btnAdd" linetype="3" @click="UserInput('tj')">
                                </btn>
                                <btn :text="col.btnDelete" linetype="4" @click="onDelete"></btn>
                            </span>
                        </el-col>
                    </el-row>
                    <el-row>
                        <!--工厂组件-->
                        <list-factorys-all v-model="formInline.model.SubFactory"></list-factorys-all>
                        <el-col :xs="12" :sm="12" :md="8" :lg="8" :xl="6">
                            <div class="elst-select-mini">
                                <el-form-item>
                                    <el-input v-model="formInline.model.Name" :placeholder="'区域名称'" size="mini">
                                        <template slot="prepend">
                                            区域名称:
                                        </template>
                                    </el-input>
                                </el-form-item>
                            </div>
                        </el-col>
                        <el-col :xs="12" :sm="12" :md="8" :lg="8" :xl="6">
                            <div class="elst-select-mini">
                                <el-form-item>
                                    <el-input v-model="formInline.model.Code" :placeholder="'区域代码'" size="mini">
                                        <template slot="prepend">
                                            区域代码:
                                        </template>
                                    </el-input>
                                </el-form-item>
                            </div>
                        </el-col>
                    </el-row>
                </el-form>
                <el-table class="ele_expand"  ref="table" stripe :data="ListItem.tableData" :height="ListItem.tableHeight" border
                          @selection-change="handleSelectionChange" row-style="height:10px" row-key="id"
                          :tree-props="{children: 'children', hasChildren: 'hasChildren'}" style="width: 100%">
                    <el-table-column type="selection" align="center" width="55">
                    </el-table-column>

                    <el-table-column type="index" :label="SysDepartment.No" :index="indexMethod" width="53"
                                     show-overflow-tooltip="true">

                    </el-table-column>
                    <el-table-column prop="name" :label="'区域名称'" show-overflow-tooltip="true" min-width="150">
                    </el-table-column>
                    <el-table-column prop="code" :label="'区域编码'" min-width="150" show-overflow-tooltip="true">
                    </el-table-column>
                    <el-table-column prop="sort" :label="'排序'" min-width="150" show-overflow-tooltip="true">
                    </el-table-column>

                    <el-table-column prop="remark" :label="SysDepartment.Remark" min-width="150"
                                     show-overflow-tooltip="true">
                    </el-table-column>
                    <el-table-column width="100" prop="update" :label="col.btnUpdate" fixed="right"
                                     v-if="authorize('SysAreaController_SaveAsync')">
                        <template slot-scope="scope">
                            <button type="button" class="elst-bt-xg" @click=UserInput('xg',scope.row.id)>
                                <i class="el-icon-edit"></i>
                            </button>
                            <button type="button" class="elst-bt-xg" @click=DutyInput('xg',scope.row.id)
                                    v-if="authorize('SysDepartmentController_SaveDutyAsync')">
                                <i class="el-icon-setting"></i>
                            </button>
                            <button type="button" class="elst-bt-xg" @click=DepartmentInput(scope.row)
                                    v-if="!scope.row.children">
                                <i class="el-icon-setting"></i>
                            </button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
        </el-row>
        <el-dialog title="区域编辑" fullscreen="true" :visible.sync="ListItem.dialogVisible" @close='closeDialog'
                   width="80%" height="80%" top="10vh">
            <iframe ref="iframe" :src="ListItem.InputUrl" style="height:95%;width:100%" frameborder="0"></iframe>
        </el-dialog>


        <el-dialog title="添加关联部门" :visible.sync="DepartmentdialogVisible" @close='DepartmentdialogClose'
                   top="30vh">
            <br>
            <el-form size="mini" label-position="right">
                <el-row>
                    <el-col :span="11">
                        <div class="elst-select-mini">
                            <el-form-item>
                                <el-input size="mini"
                                          style="border-right: 1px solid #DCDFE6;border-radius: 5px;">
                                    <template slot="prepend">
                                        {{'选择部门'+mark.m}}
                                    </template>
                                    <treeselect v-model="departmentId" :options="department" :flat="true"
                                                slot="suffix" style="    line-height: 28px; margin-left: 92px;"
                                                placeholder="请选择部门" />
                                </el-input>
                            </el-form-item>
                        </div>
                    </el-col>


                </el-row>
            </el-form>
            <br>

            <el-row type="flex" justify="end">
                <el-button type="primary" @click="SubmitDialog('ruleForm')">{{col.btnSubmit}}</el-button>
                <el-button type="primary" @click="DepartmentdialogVisible = false">{{col.btnBack}}</el-button>
            </el-row>
        </el-dialog>


    </div>
    <script type="text/javascript">
        //Vue.component('treeselect', VueTreeselect.Treeselect)
        app = new Vue({
            el: '#app',
            data: function () {
                return {
                    pageInitial: {},
                    //页面内数据初始化
                    ListItem: ListItem,
                    //部门数据集
                    department: [],
                    //部门model
                    departmentId: null,
                    DepartmentdialogVisible: false,
                    currentAreaId: null,
                    formInline: {
                        model: {
                            Name: '',//名称
                            Code: '',//编码
                            SubFactory: null,//隶属工厂
                        },
                        offset: 0,
                        limit: 1000
                    },
                    DataList: [],//首次加载数据源
                    //工厂字典
                    factory: [],
                }
            },
            mounted: function () {
                this.pageInitial = PageInitial(null, [
                    "SysAreaController_GetList",
                    "SysAreaController_SaveAsync",
                    "SysAreaController_DeleteMutiAsync",
                    "SysAreaController_Get"
                ]);
                //初始化部门下拉树数据
                this.getDepartmentData();
                //列表高度自适应
                tablehigh(this);
                this.$nextTick(function () {
                    //初始化数据
                    this.GetPageData(1);
                });
            },

            methods: {
                //解決edge下select bug
                visibleChange: function (isVisible) {
                    visibleChange(isVisible)
                },

                authorize: function (functionCode) { return authorize(this.pageInitial.authorizeList, functionCode); },
                //表格序号连续化
                indexMethod: function (index) {
                    return index + 1;
                },
                closeDialog: function () { this.GetPageData(true); this.onTable(); },
                DepartmentdialogClose: function () {
                    this.departmentId = null;
                },
                //删除方法
                onDelete: function () {
                    var mythis = this;

                    if (mythis.ListItem.tableMultipleSelection.length == 0) {
                        showMessage(msg.AtLeastOne, 'error')
                        return;
                    }
                    mythis.$confirm(' 是否确认继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(function () {
                        var delIds = [];
                        for (var i = 0; i < mythis.ListItem.tableMultipleSelection.length; i++) {
                            var item = mythis.ListItem.tableMultipleSelection[i];
                            delIds.push(item.id);
                        }
                        myAjax("post", mythis, '/api/SysArea/DeleteMuti', delIds, function (response) {
                            mythis.GetPageData();
                            mythis.onTable();
                            showMessage(msg.OperatingOk, "success");
                        });
                    }).catch(function () {
                        showMessage("已取消删除!");
                    });
                },
                //列表选择触发
                handleSelectionChange: function (val) { handleSelectionChange(this, val); },
                //Dialog展示
                UserInput: function (val, id) { DialogShow(this, val, "SysAreaInput.html", id); },
                //区域对应部门弹窗
                DepartmentInput: function (data) {
                    this.currentAreaId = data.id;
                    var mythis = this;
                    myAjax("get", mythis, '/api/SysArea/GetDepartment?id=' + this.currentAreaId, null, function (response) {
                        var ajaxModel = response.data.departId;
                        mythis.departmentId = ajaxModel;
                    }, false);


                    //this.departmentId = data.departmentId;

                    this.DepartmentdialogVisible = true;
                },
                DutyInput: function (val, id) { DialogShow(this, val, "SysDepartmentDutyInput.html", id); },
                //获取分页数据(未完成，分级筛选)
                GetPageData: function (type) {
                    var mythis = this;
                    myAjax("post", this, '/api/SysArea/GetList', mythis.formInline, function (response) {
                        mythis.ListItem.dataSource = response.data.pageData;
                    }, false);
                    //筛选后容器
                    var list = [];
                    //数据源
                    var dataSource = mythis.ListItem.dataSource;
                    //筛选条件
                    var Name = mythis.formInline.model.Name;
                    var Code = mythis.formInline.model.Code;
                    var SubFactory = mythis.formInline.model.SubFactory;
                    SubFactory = getDicValue(this.factory, SubFactory);
                    //过滤填充
                    for (var i = 0; i < dataSource.length; i++) {
                        var v = dataSource[i];
                        if (!v.isDeleted) {
                            if (Name && v.name.indexOf(Name) == -1)
                                continue;
                            if (Code && (v.code + '').indexOf(Code) == -1)
                                continue;
                            if (SubFactory && (v.subFactory + '').indexOf(SubFactory) == -1)
                                continue;
                            list.push(v);
                        }
                    }

                    if (type) {
                        if (list.length > 0) {
                            list.forEach(function (v, i) {
                                //获取父级
                                getParentNodes(v, dataSource, list);
                            })
                        }
                        //去重
                        list = uniq(list)
                    }
                    //扁平分级
                    list = menuHelper(list, guidEmpty);

                    //绑定页面表格
                    mythis.ListItem.tableData = list;
                },
                //查询方法
                onSubmit: function () {
                    this.GetPageData(true);
                    this.onTable();
                },
                //打开tableTree
                onTable: function () {
                    ////获取所有行，并去掉display:none 蠢办法，将就用
                    //var els = this.$el.getElementsByClassName('el-table__row')
                    //for (var i = 0; i < els.length; i++) {
                    //    if (els[i].style.display) {
                    //        els[i].style.display = "";
                    //    }
                    //}
                },
                //关闭tableTree
                offTable: function () {

                },
                //初始化部门下拉树(完成)
                getDepartmentData: function () {
                    var mythis = this;

                    myAjax("post", this, '/api/SysDepartment/GetList', { model: { Name: '', Code: '', CreateFactory: null }, offset: 0, limit: 10 }, function (response) {
                        mythis.department = menuHelper(response.data.pageData, guidEmpty);

                    }, false);
                },
                //提交区域与部门关系
                SubmitDialog: function (formName) {
                    console.log('this.currentAreaId', this.currentAreaId)

                    console.log('this.departmentId', this.departmentId)
                    var mythis = this;
                    myAjax("post", mythis, '/api/SysArea/SaveDepartment', { Id: this.currentAreaId, DepartmentId: this.departmentId }, function (response) {
                        showMessage(msg.OperatingOk, 'success');
                        mythis.DepartmentdialogVisible = false;
                    });
                }
            }
        });
        //步骤1：递归父级
        function getParentNodes(obj, data, result) {
            try {
                if (obj.parentId != guidEmpty && obj.parentId) {
                    data.forEach(function (v, i) {
                        if (obj.parentId == v.id) {
                            result.push(v);
                            getParentNodes(v, data, result);
                        }
                    })
                }
            } catch (e) {
                debugger;
                console.log(e);

            }

        }
        //步骤2：去重数据
        function uniq(array) {
            var temp = [];
            for (var i = 0; i < array.length; i++) {
                //如果当前数组的第i项在当前数组中第一次出现的位置是i，才存入数组；否则代表是重复的
                if (array.indexOf(array[i]) == i) {
                    temp.push(array[i])
                }
            }
            return temp;
        }
        //步骤3：数据序列化(tabletree)
        //function menuHelper(list, pid) {
        //    var result = [];
        //    if (list.length <= 0)
        //        return list;
        //    list.forEach(function (v, i) {
        //        var temp = {};
        //        for (var key in v) {
        //            temp[key] = v[key];
        //        }
        //        temp.parentId = temp.parentId ? temp.parentId : guidEmpty;
        //        var obj;
        //        if (temp.parentId == pid) {
        //            obj = temp;
        //            var nextObj = menuHelper(list, temp.id);
        //            if (nextObj.length > 0) {
        //                obj.children = nextObj;
        //            }
        //        }
        //        if (obj)
        //            result.push(obj)
        //    })
        //    return result;
        //}
        function menuHelper(list, pid) {
            var result = [];
            if (list.length <= 0)
                return list;
            list.forEach(function (v, i) {
                var temp = {};
                for (var key in v) {
                    temp[key] = v[key];
                }
                temp.label = v.name;
                temp.parentId = temp.parentId ? temp.parentId : guidEmpty;
                var obj;
                if (temp.parentId == pid) {
                    obj = temp;
                    var nextObj = menuHelper(list, temp.id);
                    if (nextObj.length > 0) {
                        obj.children = nextObj;
                    }
                }
                if (obj)
                    result.push(obj)
            })
            return result;
        }
        //data:字典数据集，key:键
        function getDicValue(data, key) {
            var val = key;
            if (data.length > 0) {
                data.forEach(function (o, j) {
                    if (o.key == key) {
                        val = o.value;
                        return;
                    }
                });
            }
            return val;
        }
    </script>
</body>

</html>